$(function (){
  var list;
     function getMyDate(dateArr){
      var newArr = dateArr[0].days;
      list = newArr;
      // return list;
      alert(list)
     }
  $('#calendar').calendar({
    ifSwitch: true,
    hoverDate: true,
    backToday: true,
  });
});
;(function ($, window, document, undefined) {
  var Calendar = function (elem, options) {
    this.$calendar = elem;
    this.defaults = {
      ifSwitch: true,
      hoverDate: false,
      backToday: false

    };
    this.opts = $.extend({}, this.defaults, options);
  };
  Calendar.prototype = {
    showHoverInfo: function (obj) {
      var _dateStr = $(obj).attr('data');
      var offset_t = $(obj).offset().top + (this.$calendar_today.height() - $(obj).height()) / 2;
      var offset_l = $(obj).offset().left + $(obj).width();
      var changeStr = addMark(_dateStr);
      var _week = changingStr(changeStr).getDay();
      var _weekStr = '';
      this.$calendar_today.show();
      this.$calendar_today
            .css({left: offset_l + 30, top: offset_t})
            .stop()
            .animate({left: offset_l + 16, top: offset_t, opacity: 1});

      switch(_week) {
        case 0:
          _weekStr = '星期日';
        break;
        case 1:
          _weekStr = '星期一';
        break;
        case 2:
          _weekStr = '星期二';
        break;
        case 3:
          _weekStr = '星期三';
        break;
        case 4:
          _weekStr = '星期四';
        break;
        case 5:
          _weekStr = '星期五';
        break;
        case 6:
          _weekStr = '星期六';
        break;
      }
      this.$calendarToday_date.text(changeStr);
      this.$calendarToday_week.text(_weekStr);
    },
    showCalendar: function () {
      var self = this;
      var year = dateObj.getDate().getFullYear();
      var month = dateObj.getDate().getMonth() + 1;
      var dateStr = returnDateStr(dateObj.getDate());
      var firstDay = new Date(year, month - 1, 1);

      this.$calendarTitle_text.text(year + '/' + dateStr.substr(5, 2));
      this.$backToday.text(dateStr.substr(6, 2));
      this.$calendarDate_item.each(function (i) {
        var allDay = new Date(year, month - 1, i + 1 - firstDay.getDay());
        var allDay_str = returnDateStr(allDay);
        $(this).text(allDay.getDate()).attr('data', allDay_str);
        if (returnDateStr(firstDay).substr(0, 7) === allDay_str.substr(0, 7)) {
          $(this).attr('class', 'item item-curMonth');
        } else {
          $(this).attr('class', 'item disabled unhide');
        }
      });
    },
    renderDOM: function () {
      this.$calendar_title = $('<div class="calendar-title"></div>');
      this.$calendar_week = $('<ul class="calendar-week"></ul>');
      this.$calendar_date = $('<ul class="calendar-date"></ul>');
      this.$calendar_today = $('<div class="calendar-today"></div>');
      var _titleStr = '<a href="#" class="title"></a>'+
                      // '<a href="javascript:;" id="backToday">T</a>'+
                      '<div class="arrow">'+
                        '<span class="arrow-prev"><</span>'+
                        '<span class="arrow-next">></span>'+
                      '</div>';
      var _weekStr = '<li class="item" class="key">日</li>'+
                      '<li class="item">一</li>'+
                      '<li class="item">二</li>'+
                      '<li class="item">三</li>'+
                      '<li class="item">四</li>'+
                      '<li class="item">五</li>'+
                      '<li class="item" class="key">六</li>';
      var _dateStr = '';
      var _dayStr = '<i class="triangle"></i>'+
                    '<p class="date"></p>'+
                    '<p class="week"></p>';

      for (var i = 0; i < 6; i++) {
        _dateStr += '<li class="item">26</li>'+
                    '<li class="item">26</li>'+
                    '<li class="item">26</li>'+
                    '<li class="item">26</li>'+
                    '<li class="item">26</li>'+
                    '<li class="item">26</li>'+
                    '<li class="item">26</li>';
      }
      this.$calendar_title.html(_titleStr);
      this.$calendar_week.html(_weekStr);
      this.$calendar_date.html(_dateStr);
      this.$calendar_today.html(_dayStr);
      this.$calendar.append(this.$calendar_title, this.$calendar_week, this.$calendar_date/*, this.$calendar_today*/);
      this.$calendar.show();
    },
    inital: function () {
      var self = this;
      this.renderDOM();
      this.$calendarTitle_text = this.$calendar_title.find('.title');
      this.$backToday = $('#backToday');
      this.$arrow_prev = this.$calendar_title.find('.arrow-prev');
      this.$arrow_next = this.$calendar_title.find('.arrow-next');
      this.$calendarDate_item = this.$calendar_date.find('.item');
      this.$calendarToday_date = this.$calendar_today.find('.date');
      this.$calendarToday_week = this.$calendar_today.find('.week');
      this.showCalendar();
      if (this.opts.ifSwitch) {
        this.$arrow_prev.bind('click', function () {
          var _date = dateObj.getDate();
          dateObj.setDate(new Date(_date.getFullYear(), _date.getMonth() - 1, 1));
          self.showCalendar();
          $(".calendar-date").find("li").css({"color":"#d2d2d2"});
          change();
        });
        this.$arrow_next.bind('click', function () {
          var _date = dateObj.getDate();
          dateObj.setDate(new Date(_date.getFullYear(), _date.getMonth() + 1, 1));
          self.showCalendar();
          $(".calendar-date").find("li").css({"color":"#d2d2d2"});
          change();
        });
      };

      $(".section_right .calendar-date").on("click","li",function(event){
        var text1=$(this).text();
        var i = $(this).index();
        if(text1 < 10){
          text1 = '0' + text1;
        }
        $(this).parents(".section_right").prev().find(".top_1").eq(i).show().siblings().hide();
        /*if($(".ul").is(':hidden') == true){
          if($(this).parents(".section_right").prev().find(".top_1").eq(i).html().trim()==""){
            $(this).css({"color":"#2a63c1"}) ;
          }else{
            $(this).css({"color":"#888"});
          }
        }else if( $("#nomsg").is(':hidden') == true){
            $(this).css({"color":"#888"});
        }*/

        $(this).addClass("liactive").siblings().removeClass("liactive");
        $('#backToday').text(text1);
        $(this).addClass('item-curDay').siblings('li').removeClass('item-curDay')
      });

      change();

      //划过结束
      this.$calendarDate_item.hover(function () {
        self.showHoverInfo($(this));
      }, function () {
        self.$calendar_today.css({left: 0, top: 0}).hide();
      });
    },
    constructor: Calendar
  };
  $.fn.calendar = function (options) {
    var calendar = new Calendar(this, options);
    return calendar.inital();
  };
  var dateObj = (function () {
    var _date = new Date();
    return {
      getDate: function () {
        return _date;
      },
      setDate: function (date) {
        _date = date;
      }
    }
  })();
  function returnDateStr(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    month = month <= 9 ? ('0' + month) : ('' + month);
    day = day <= 9 ? ('0' + day) : ('' + day);
    return year + "-" + month + "-" + day;
  };
  function changingStr(fDate) {
    var fullDate = fDate.split("-");
    return new Date(fullDate[0], fullDate[1] - 1, fullDate[2]);
  };
  function addMark(dateStr) { // 给传进来的日期字符串加-
    return dateStr.substr(0, 4) + '-' + dateStr.substr(4, 2) + '-' + dateStr.substring(6);
  };

})(jQuery, window, document);
function change(){
    var arr = [];
    // 获取日期信息
    $.each($(".calendar-date").find("li"),function(key,val){
        for(var i = 0; i < list.length; i++){
            if($(val).attr("data") == list[i]){
                $(val).addClass("activeDate");
                $(val).css("color","#333");
            }
        }
    });
}
